package com.epoint.androidphone.mobileoa.ui.todo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

import com.epoint.androidmobile.core.control.AlertUtil;
import com.epoint.androidmobile.core.control.ToastUtil;
import com.epoint.androidmobile.core.string.StringHelp;
import com.epoint.androidmobile.core.xml.XMLUtil;
import com.epoint.androidphone.R;
import com.epoint.androidphone.mobileoa.ui.init.InitView;
import com.epoint.androidphone.mobileoa.ui.superview.SuperPhonePage;
import com.epoint.androidmobile.bizlogic.common.FileDownService;
import com.epoint.androidmobile.bizlogic.todo.task.Handle_GetArchive_Task;
import com.epoint.androidmobile.bizlogic.todo.model.AttFileInfoModel;
import com.epoint.androidmobile.bizlogic.todo.model.FlowInfoModel;
import com.epoint.androidmobile.bizlogic.todo.model.NextStepInfoModel;
import com.epoint.androidmobile.bizlogic.todo.model.PreStepInfoModel;

/**
 * @author liyc
 * @time 2013-5-8 下午5:01:49
 * @annomation 待办详情
 */
public class TodoDetailView extends SuperPhonePage {
	
	long getHandleGetArchiveTaskID;
	WebView wvZW,wvBZ;
	Button btZw,btBz,btFj;
	List<AttFileInfoModel> attList = new ArrayList<AttFileInfoModel>();
	List<NextStepInfoModel> NextStepInfoList = new ArrayList<NextStepInfoModel>();
	List<PreStepInfoModel> presList = new ArrayList<PreStepInfoModel>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addContentViewAndTitle(R.layout.tododetailview, "待办详情");
		wvZW = (WebView) findViewById(R.id.wvZW);
		getLlContent().setBackgroundResource(R.color.white);
		wvBZ = (WebView) findViewById(R.id.wvBz);
		
		btZw = (Button) findViewById(R.id.btZw);
		btZw.setOnClickListener(this);
		btBz = (Button) findViewById(R.id.btBz);
		btBz.setOnClickListener(this);
		btFj = (Button) findViewById(R.id.btFj);
		btFj.setOnClickListener(this);
		
		getBtRight().setVisibility(View.VISIBLE);
		getBtRight().setText("操作");
		getData();
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		if(v==btZw)
		{
			wvZW.setVisibility(View.VISIBLE);
			wvBZ.setVisibility(View.GONE);
		}
		else if(v==btBz)
		{
			wvZW.setVisibility(View.GONE);
			wvBZ.setVisibility(View.VISIBLE);
		}
		else if(v==btFj)
		{
			if(attList.size()==0)
			{
				return;
			}
			String[] attstr = new String[attList.size()];
			for(int i=0;i<attList.size();i++)
			{
				attstr[i] = attList.get(i).AttFileName;
			}
			AlertUtil.showAlertMenu(this, "附件下载", attstr, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					AttFileInfoModel m = attList.get(which);
					FileDownService.download(getContext(), getString(R.string.WebServiceType),m.DownLoadUrl, m.isBigFile, getIntent().getStringExtra("MessageItemGuid"), m.AttFileName, InitView.attachdownMainPath);
				}
			});
		
		}
		else if(v==getBtRight())
		{
			final int allStepsCount = presList.size()+NextStepInfoList.size();
			String[] steps = new String[allStepsCount];
			
			for(int i=0;i<presList.size();i++)
			{
				steps[i] = presList.get(i).StepName;
			}
			
			for(int i=0;i<NextStepInfoList.size();i++)
			{
				steps[i+presList.size()] = NextStepInfoList.get(i).StepName;
			}
			
			AlertUtil.showAlertMenu(this, "操作", steps, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(which<presList.size())
					{
						System.out.println("退回步骤");
					}
					else{
						Intent intent = new Intent(getContext(), TodoNextStepView.class);
						intent.putExtra("NextStepInfoModel", NextStepInfoList.get(allStepsCount-which-1));
						intent.putExtra("MessageItemGuid", getIntent().getStringExtra("MessageItemGuid"));
						startActivityForResult(intent, 1001);
					}
				}
			});
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==1001){
			switch (resultCode) {
			case RESULT_OK:
				finish();
				break;
			default:
				break;
			}
		}
	}
	
	private void getData()
	{
		showTopProgressBar();
		HashMap<String, Object> params = getPassMap();
		params.put("MessageItemGuid", getIntent().getStringExtra("MessageItemGuid"));
		getHandleGetArchiveTaskID = new Handle_GetArchive_Task(this, params).startTask();
	}
	
	@Override
	public void refreshUI(long taskId, Object obj) {
		super.refreshUI(taskId, obj);
		if(taskId==getHandleGetArchiveTaskID)
		{
			if(validateXML(obj))
			{
				String bs = obj.toString();
				String FormShowUrl = StringHelp.getXMLAtt(bs, "FormShowUrl");
				wvZW.loadUrl(FormShowUrl);
				
				String FlowInfoLstXML = StringHelp.getXMLAttOut(bs, "FlowInfoLst");
				List<FlowInfoModel> flowList = XMLUtil.DomAnalysisCommon(FlowInfoLstXML, FlowInfoModel.class);
				String bsstr = Handle_GetArchive_Task.pingProcessXML_WS(flowList);
				wvBZ.loadDataWithBaseURL(null,
						bsstr, "text/html", "utf-8",
						null);
				
				String AttFileInfoLstXML = StringHelp.getXMLAttOut(bs, "AttFileInfoLst");
				attList = XMLUtil.DomAnalysisCommon(AttFileInfoLstXML, AttFileInfoModel.class);
				if(attList.size()>0)
				{
					btFj.setText("附件("+attList.size()+")");
				}
				
				//按钮
				String NextStepInfoLstXML = StringHelp.getXMLAttOut(bs, "NextStepList");
				try {
					NextStepInfoList = Handle_GetArchive_Task.getNextStepInfoList(NextStepInfoLstXML);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				//退回按钮
				String PreStepListXML = StringHelp.getXMLAttOut(bs, "PreStepList");
				presList = XMLUtil.DomAnalysisCommon(PreStepListXML, PreStepInfoModel.class);
				System.out.println("退回按钮："+presList.size());
				
				
				String CanPDFSign = StringHelp.getXMLAtt(bs, "CanPDFSign");
				ToastUtil.toastLong(this, "允许手写："+CanPDFSign);
				
				String PDFFileContent = StringHelp.getXMLAtt(bs, "PDFFileContent");
			}
		}
	}
}
